package org.azmat.mybatis.Controller;


import org.azmat.mybatis.SQL.Entity.Student;
import org.azmat.mybatis.SQL.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class StudentController {
    @Autowired
    StudentService studentService;

    @GetMapping("/student/{id}")
    public ModelAndView getStudentById(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("student", studentService.findStudent(id));
        mv.setViewName("student");
        return mv;
    }

    @GetMapping("/students/update")
    public ModelAndView updateStudent(Student student) {
        ModelAndView mv = new ModelAndView();
        studentService.updateStudent(student);
        mv.setViewName("redirect:/student/" + student.getId());
        return mv;

    }

    @GetMapping("/students/{id}")
    public ModelAndView getStudent(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("student", studentService.findStudentById(id));
        mv.setViewName("student"); // view name
        return mv;
    }

}
